<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>权限管理</el-breadcrumb-item>
        <el-breadcrumb-item>权限列表</el-breadcrumb-item>
    </el-breadcrumb> 
    <el-card>
        <el-button type="primary">添加角色</el-button>
        <el-table  :data="rolesRight"  border stripe>
            <el-table-column type="expand">
                <!-- 展开权限列表 -->
                <template slot-scope="scope">
                    <el-row :class="['bdbottom',i1==0?'bdtop':'','verter']" v-for="(item1,i1) in scope.row.children" :key="item1.id">
                        <el-col :span="5">
                            <el-tag  closable 
                                        @close='closeTagRight(scope.row,item1.id)'>{{item1.authName}}</el-tag>
                            <i class="el-icon-caret-right"></i>
                        </el-col>
                        <el-col :span="19">
                            <el-row :class="[i2!==0?'bdtop':'','verter']" v-for="(item2,i2) in item1.children" :key="item2.id">
                                <el-col :span="7">
                                    <el-tag type="success"  closable 
                                        @close='closeTagRight(scope.row,item2.id)'>
                                        {{item2.authName}}
                                    </el-tag>
                                    <i class="el-icon-caret-right"></i>
                                    </el-col>  
                                    <el-col :span="12">
                                        <el-tag type="warning" 
                                        v-for="item3 in item2.children" 
                                        :key="item3.id" 
                                        closable 
                                        @close='closeTagRight(scope.row,item3.id)'>
                                            {{item3.authName}}
                                        </el-tag>
                                    </el-col>
                            </el-row>
                        </el-col>
                    </el-row>    
                </template>
            </el-table-column>
            <el-table-column type="index" prop="id">
            </el-table-column>
            <el-table-column prop="roleName" label="角色名称" >
            </el-table-column>
            <el-table-column prop="roleDesc"  label="角色描述" >
            </el-table-column>
            <el-table-column prop="level" label="操作">
                <template slot-scope="scope">
                    <el-button size='mini' type="primary" icon="el-icon-edit">编辑</el-button>
                    <el-button size='mini' type="warning" icon="el-icon-delete">删除</el-button>
                    <el-button size='mini' type="success" icon="el-icon-setting" @click="showSetDialog(scope.row)">分配权限</el-button>
                </template>
            </el-table-column>
        </el-table>
    </el-card>
    <!-- 权限分配对画框 -->
    <el-dialog
        title="提示"
        :visible.sync="setRoleRightDialog"
        width="50%"
        @close='setDialogClose'
        >
        <el-tree ref="treeRef" :data="rightList" node-key='id' :default-checked-keys='defaultKeys'  :props="treeProps" show-checkbox default-expand-all></el-tree>
        <span slot="footer" class="dialog-footer">
            <el-button @click="setRoleRightDialog = false">取 消</el-button>
            <el-button type="primary" @click="setRights">确 定</el-button>
        </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
    data(){
        return{
            rolesRight:[],
            setRoleRightDialog:false,
            rightList:[],
            treeProps:{
                children: 'children',
                label:'authName'
            },
            defaultKeys:[],
            roleId:''
        }
    },
    created(){
        this.getRolesRight()
    },
    methods:{
        async getRolesRight(){
           const {data:res}= await this.$http.get('roles')
        //    console.log(res)
           this.rolesRight = res.data
        },
        async closeTagRight(roleId,id){
         const resultR =await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
            }).catch(err=>err)
            if(resultR!=='confirm') return this.$message.info('取消了删除')
            const {data:res} = await this.$http.delete(`roles/${roleId.id}/rights/${id}`)
            // console.log(res)
            roleId.children = res.data
            // this.getRolesRight()
        },
        async showSetDialog(role){
            this.roleId = role.id
           const {data:res} =await this.$http.get('rights/tree')
           console.log(res)
           this.rightList = res.data
           this.getThreeRight(role,this.defaultKeys)
            this.setRoleRightDialog=true
            
        },
        getThreeRight(node,arr){
            if(!node.children){
                return arr.push(node.id)
            }else{
                node.children.forEach(item=>{
                    this.getThreeRight(item,arr)
                })
            }
        },
        setDialogClose(){
            this.defaultKeys=[]
        },
        async setRights(){
            const keys = [...this.$refs.treeRef.getCheckedKeys(),
                        ...this.$refs.treeRef.getHalfCheckedKeys()
                        ]
                
                // console.log(keys)
            const strKeys = keys.join(',')
            const {data:res} =await this.$http.post(`roles/${this.roleId}/rights`,{rids :strKeys})

            console.log(res)
            if(res.meta.status!==200)return this.$message.err("添加失败")
            this.getRolesRight()
            this.setRoleRightDialog = false
        }
    }
}
</script>

<style>
.el-button{
    margin-bottom: 15px;;
}
.el-tag{
    margin: 7px;
}
.bdtop{
    border-top: 1px solid #eee;
}
.bdbottom{
    border-bottom:1px solid #eee ;
}
.verter{
    display: flex;
    align-items: center;
}
</style>